<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/7/17
 * Time: 10:51
 */
namespace app\home\controller;
//include '/data/wwwroot/health.xuanwumobile.com/vendor/autoload.php'; // 引入 composer 入口文件
// 一些配置
use think\Db;
use think\Config;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use think\Controller;

class Index extends Common
{
    public function index()
    {
        session('target_url',"http://mww5.xuanwumobile.com/home/Index/index");
        $openid = $this->getOpenid();
        $need = 0;
        $address = '';
        $data = array();
        if(isset(session('location')['lat']) && session('location')['lat']){
            $need = 1;
            $address = session('location')['address'];
            $lat = session('location')['lat'];
            $lon = session('location')['lon'];
            $where = array();
            $where['s_status'] = 1;//不能是信用太低的
            $where['s_join_end'] = array('>=',date('Y-m-d'));//到期时间大于今天
            $mile = Db::table('up_set_juli')->where('juli_id',1)->value('juli_value');
            $miles = (int)$mile*1000;
            $field = 's_id,s_thumb,s_name,s_pinlei,pl_image';
            $field .= ',ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(('.$lat.'* PI() / 180 - s_lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(s_lat * PI() / 180) * POW(SIN(('.$lon.' * PI() / 180 - s_lon * PI() / 180) / 2),2))) * 1000) AS distance';
            $data = Db::table('up_shops')->alias('s')
                ->join('up_work_pl wp','s.s_pinlei_id=wp.pl_id','LEFT')
                ->field($field)
                ->where($where)
                ->having("distance <= $miles")
                ->order('distance ASC')
                ->page(1,15)
                ->select();
        }
        $this->assign('need',$need);
        $this->assign('address',$address);
        $this->assign('data',$data);
        $index = Db::table('up_tan')->where('tan_id',4)->find();
        $this->assign('index',$index['tan_image']);
        return $this->fetch('index/index');
    }

    /**
     * 其实作用就是把经纬度信息入缓存
     * @return \think\response\Json
     */
    public function indexGetData()
    {
        extract(input());
        $ses = array();
        $ses['lat'] = $lat;
        $ses['lon'] = $lon;
        $ses['address'] = $address;
        if(isset($aid)){
            $ses['aid'] = $aid;
        }else{
            $ses['aid'] = 0;
        }
        session('location',$ses);
        $where = array();
        $where['s_status'] = 1;//不能是信用太低的
        $where['s_join_end'] = array('>=',date('Y-m-d'));//到期时间大于今天
        $mile = Db::table('up_set_juli')->where('juli_id',1)->value('juli_value');
        $miles = (int)$mile*1000;
        $field = 's_id,s_thumb,s_name,s_pinlei';
        $field .= ',ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(('.$lat.'* PI() / 180 - s_lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(s_lat * PI() / 180) * POW(SIN(('.$lon.' * PI() / 180 - s_lon * PI() / 180) / 2),2))) * 1000) AS distance';
        $data = Db::table('up_shops')
            ->field($field)
            ->where($where)
            ->having("distance <= $miles")
            ->order('distance ASC')
            ->page(1,15)
            ->select();
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 某个商家详情——首页
     * s_id 商家id
     * lat lon
     * @return array 返回数组
     */
    public function shopInfo()
    {
        extract(input());
        $auth = $this->checkAll();
        $user_id = 0;
        //-------------------------------------------------
        session('target_url',"http://mww5.xuanwumobile.com/home/Index/shopInfo?s_id={$s_id}");
        $openid = $this->getOpenid();
        $user = Db::table('up_u_user')
            ->where('user_openid', $openid)
            ->find();
        if($user){
            $user_id = $user['user_id'];
        }
        //-------------------------------------------------
        $lat = session('location')['lat'];
        $lon = session('location')['lon'];
        $address = session('location')['address'];
        $advert = Db::table('up_shop_advert')
            ->where('adv_shopid', $s_id)
            ->select();
        $type = Db::table('up_shop_type')
            ->where('st_shopid', $s_id)
            ->order('st_order DESC')
            ->select();
        $good = Db::table('up_goods')
            ->where('g_shop', $s_id)
            ->where('g_status', 1)
            ->order('g_time DESC')
//            ->page(1,12)
            ->select();
        //----------------加入购物车了没-----
        if($good){
            foreach ($good as $k=>$v){
                $good[$k]['haveNum'] = 0;
                $have = Db::table('up_shopping_car')
                    ->where('car_goodid', $v['g_id'])
                    ->where('car_userid', $user_id)
                    ->field('car_number')
                    ->find();
                if($have){
                    $good[$k]['haveNum'] = $have['car_number'];
                }
            }
        }
        //----------------加入购物车了没end-----
        $shop = Db::table('up_shops')
            ->where('s_id', $s_id)
            ->find();
        //------------------------------------------------
        $where = array();
        $where['s_status'] = 1;//不能是信用太低的
        if(isset($keyword) && $keyword){
            $where['s_name'] = array('like',"%$keyword%");
        }
        $mile = Db::table('up_set_juli')->where('juli_id',1)->value('juli_value');
        $miles = $mile*1000;
        $field = 's_id,s_thumb,s_name,s_pinlei';
        $field .= ',ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(('.$lat.'* PI() / 180 - s_lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(s_lat * PI() / 180) * POW(SIN(('.$lon.' * PI() / 180 - s_lon * PI() / 180) / 2),2))) * 1000) AS distance';
        $data = Db::table('up_shops')
            ->field($field)
            ->where($where)
            ->having("distance <= $miles")
            ->order('distance ASC')
            ->page(1,15)
            ->select();
        //------------------------------------------------
        $this->assign('advert',$advert);
        $this->assign('type',$type);
        $this->assign('good',$good);
        $this->assign('shop',$shop);
        $this->assign('data',$data);
        $this->assign('address',$address);
        $this->assign('s_id',$s_id);
        $now = explode(':',date('H:i'));
        $self = $now[0]*60+$now[1];
        $open = explode(':',$shop['s_open']);
        $close = explode(':',$shop['s_close']);
        $qian = $open[0]*60+$open[1];
        $hou = $close[0]*60+$close[1];
        $chaochu = 0;//不显示
        if($self < $qian || $self > $hou){
            $chaochu = 1;//显示
        }
        $this->assign('chaochu',$chaochu);
        return $this->fetch('index/shopinfo');
    }

    /**
     *
     * @return \think\response\Json
     */
    public function address(){
        extract(input());
        $auth = $this->checkAll();
        //-------------------------------------------------
        session('target_url',"http://mww5.xuanwumobile.com/home/Index/address");
        $openid = $this->getOpenid();
        $user = Db::table('up_u_user')
            ->where('user_openid', $openid)
            ->find();
        if(!$user){
            return $this->fetch('login/login');
        }else{
            $user_id = $user['user_id'];
            $address = Db::table('up_user_address')//存在即代表有抢购商品（秒杀）
            ->where('add_userid', $user_id)
                ->order('add_id DESC')
                ->select();
            $this->assign('address',$address);
            $this->assign('type',1);
            return $this->fetch('my/address');
        }
        //-------------------------------------------------
    }
    /**
     * @return \think\response\Json
     */
    public function addAdd(){
        extract(input());
        $data = array();
        $data['name'] = '';
        $data['phone'] = '';
        $data['sex'] = '';
        $data['lat'] = '';
        $data['lon'] = '';
        $data['address'] = '';
        $data['door'] = '';
        if(isset($name)){$data['name'] = $name;}
        if(isset($phone)){$data['phone'] = $phone;}
        if(isset($sex)){$data['sex'] = $sex;}
        if(isset($lat)){$data['lat'] = $lat;}
        if(isset($lon)){$data['lon'] = $lon;}
        if(isset($address)){$data['address'] = $address;}
        if(isset($door)){$data['door'] = $door;}
        $this->assign('data',$data);
        $this->assign('type',1);
        $this->assign('addEdit',1);
        return $this->fetch('my/add_address');
    }
    public function addressLocation(){
        extract(input());
        $data = array();
        $data['name'] = '';
        $data['phone'] = '';
        $data['sex'] = '';
        $data['lat'] = '';
        $data['lon'] = '';
        $data['address'] = '';
        $data['door'] = '';
        if(isset($name)){$data['name'] = $name;}
        if(isset($phone)){$data['phone'] = $phone;}
        if(isset($sex)){$data['sex'] = $sex;}
        if(isset($lat)){$data['lat'] = $lat;}
        if(isset($lon)){$data['lon'] = $lon;}
        if(isset($address)){$data['address'] = $address;}
        if(isset($door)){$data['door'] = $door;}
        $this->assign('type',1);
        $this->assign('data',$data);
        $this->assign('addEdit',1);
        return $this->fetch('my/location_address');
    }
    public function addMyAddress(){
        extract(input());
        $auth = $this->checkAll();
        //-------------------------------------------------
        $openid = $this->getOpenid();
        $user_id = Db::table('up_u_user')
            ->where('user_openid', $openid)
            ->value('user_id');
        $data = array();
        $data['add_userid'] = $user_id;
        $data['add_lat'] = $lat;
        $data['add_lon'] = $lon;
        $data['add_address'] = $address.$door;
        $data['add_phone'] = $phone;
        $data['add_username'] = $name;
        $data['add_sex'] = $sex;
        $res = Db::table('up_user_address')->insert($data);
        if($res){
            return json(['status'=>200,'msg'=>'成功']);
        }else{
            return json(['status'=>0,'msg'=>'失败']);
        }
    }
    /**
     * 消息通知展示
     * @return \think\response\Json
     */
    public function message(){
        extract(input());
        $auth = $this->checkAll();
        //-------------------------------------------------
        session('target_url',"http://mww5.xuanwumobile.com/home/Index/message");
        $openid = $this->getOpenid();
        $user = Db::table('up_u_user')
            ->where('user_openid', $openid)
            ->find();
        if(!$user){
            return $this->fetch('login/login');
        }else{
            $user_id = $user['user_id'];
            $data = Db::table('up_notice')
                ->where('n_type',1)
                ->where('n_app',1)
                ->where('n_delete','not like','%,'.$user_id.',%')
                ->order('n_time DESC')
                ->page(1,15)
                ->select();
            if($data){
                foreach ($data as $k=>$v){
                    $data[$k]['read'] = 0;
                    if(strpos($v['n_read'],",$user_id,") !== false){
                        $data[$k]['read'] = 1;
                    }
                }
            }
            $this->assign('data',$data);
            $this->assign('user_id',$user_id);
            return $this->fetch('index/notice');
        }
        //-------------------------------------------------
    }
    /**
     * 消息通知列表:notice
     * type $page
     */
    public function noticeList(){
        extract(input());
        if($type == 1){
            $data = Db::table('up_notice')
                ->where('n_type',1)
                ->where('n_app',1)
                ->where('n_delete','not like','%,'.$user_id.',%')
                ->order('n_time DESC')
                ->page($page,15)
                ->select();
            if($data){
                foreach ($data as $k=>$v){
                    $data[$k]['read'] = 0;
                    if(strpos($v['n_read'],",$user_id,") !== false){
                        $data[$k]['read'] = 1;
                    }
                }
            }
        }else{
            $data = Db::table('up_notice')
                ->where('n_type',2)
                ->where('n_user_id',$user_id)
                ->order('n_time DESC')
                ->page($page,15)
                ->select();
        }
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 删除某个消息通知:delNotice
     * n_id
     */
    public function delNotice(){
        extract(input());
        $notice = Db::table('up_notice')->where('n_id',$n_id)->find();
        $type = $notice['n_type'];
        if($type == 1){
            $del = $notice['n_delete'];
            $del = $del . $user_id . ',';
            $res = Db::table('up_notice')->where('n_id',$n_id)
                ->update(['n_delete'=>$del]);
        }else{
            $res = Db::table('up_notice')->where('n_id',$n_id)
                ->delete();
        }
        if($res){
            return json(['status'=>200,'msg'=>'成功']);
        }else{
            return json(['status'=>0,'msg'=>'删除失败']);
        }
    }
    /**
     * 消息通知详情
     * n_id
     */
    public function noticeInfo(){
        extract(input());
        $notice = Db::table('up_notice')->where('n_id',$n_id)->find();
        $type = $notice['n_type'];
        if($type == 1){
            $read = $notice['n_read'];
            if(strpos($read,",$user_id,") !== false){
                $read = 1;
            }else{
                $read = $read . $user_id . ',';
                Db::table('up_notice')->where('n_id',$n_id)
                    ->update(['n_read'=>$read]);
            }
        }else{
            Db::table('up_notice')->where('n_id',$n_id)
                ->update(['n_read'=>1]);
        }
        $this->assign('data',$notice);
        $this->assign('user_id',$user_id);
        return $this->fetch('index/notice_info');
    }
    /**
     * 商户主页
     * s_id 商家id
     * @return array 返回数组
     */
    public function shopInfoMain()
    {
        extract(input());
        $this->checkAll();
        //1、商户主页
        $shop = Db::table('up_shops')
            ->where('s_id', $s_id)
            ->find();
        if ($shop['s_image']) {
            $shop['s_image'] = explode(',', $shop['s_image']);
        }
        $today = date('Y-m-d');
        //2-1、商户活动信息
        $active = Db::table('up_shop_active')
            ->where('sa_shopid', $s_id)
            ->where('sa_start', '<=',$today)
            ->where('sa_end', '>=',$today)
            ->find();
        //2-2、商户活动展示是否有抢购秒杀
        $kill = 0;
        $killhave = Db::table('up_goods')//存在即代表有抢购商品（秒杀）
        ->where('g_shop', $s_id)
            ->where('g_prcie_kill', '>',0)
            ->where('g_kill_start', '<=',$today)
            ->where('g_kill_end', '>=',$today)
            ->value('g_id');
        if($killhave){
            $kill = 1;
        }
        //3、此店铺的最新的一个评论
        $field = 'comment_id,comment_goodid,comment_user_id,comment_score,comment_content,comment_time';
        $field .= ',comment_username,comment_image,user_username,user_image';
        $pl = Db::table('up_good_comment')->alias('gc')
            ->join('up_u_user u','gc.comment_user_id=u.user_id','LEFT')
            ->where('comment_shopid', $s_id)
            ->order('comment_time DESC')
            ->field($field)
            ->find();
        $this->assign('shop',$shop);
        $this->assign('active',$active);
        $this->assign('kill',$kill);
        $this->assign('pl',$pl);
        return $this->fetch('index/main');
    }
    /**
     * 某个商家的某个类别的商品列表
     * s_id 商家id
     * type 商品类别
     * $keyword
     * @return array 返回数组
     */
    public function goSearch()
    {
        extract(input());
        $this->assign('good',array());
        $this->assign('s_id',$s_id);
        return $this->fetch('index/search');
    }
    /**
     * 某个商家的某个类别的商品列表
     * s_id 商家id
     * type 商品类别
     * name
     * $keyword
     * @return array 返回数组
     */
    public function shopGoodList()
    {
        extract(input());
        $this->checkAll();
        $user_id = 0;
        //-------------------------------------------------
        $openid = $this->getOpenid();
        $user = Db::table('up_u_user')
            ->where('user_openid', $openid)
            ->find();
        if($user){
            $user_id = $user['user_id'];
        }
        if(!isset($page)){
            $page = 1;
        }
        //1、商品列表
        $where = array();
        if(isset($type) && $type>0){
            $where['g_type'] = $type;
        }
        $where['g_shop'] = $s_id;
        $where['g_status'] = 1;
        if(isset($keyword) && $keyword){
            $where['g_name'] = array('like',"%$keyword%");
        }
        if(!isset($price)){$price = 1;}
        if(!isset($sales)){$sales = 1;}
        $order = 'g_prcie_kill DESC';
        if($price == 1){//1是高到低 2是低到高
            $order .= ',g_price_now DESC';
        }
        if($price == 2){//1是高到低 2是低到高
            $order .= ',g_price_now ASC';
        }
        if($sales == 1){//1是高到低 2是低到高
            $order .= ',g_sale_num DESC';
        }
        if($sales == 2){//1是高到低 2是低到高
            $order .= ',g_sale_num ASC';
        }
        $order = trim($order,',');
        $good = Db::table('up_goods')//存在即代表有抢购商品（秒杀）
        ->where($where)
            ->order($order)
            ->page($page,20)
            ->select();
        //----------------加入购物车了没-----
        if($good){
            foreach ($good as $k=>$v){
                $good[$k]['haveNum'] = 0;
                $have = Db::table('up_shopping_car')
                    ->where('car_goodid', $v['g_id'])
                    ->where('car_userid', $user_id)
                    ->field('car_number')
                    ->find();
                if($have){
                    $good[$k]['haveNum'] = $have['car_number'];
                }
            }
        }
        $types = Db::table('up_shop_type')
            ->where('st_shopid', $s_id)
            ->select();
        //----------------加入购物车了没end-----
        if(isset($ajax) && $ajax == 777){
            return json(['status'=>200,'msg'=>$good]);
        }else{
            $this->assign('good',$good);
            $this->assign('types',$types);
            $this->assign('type',$type);
            $this->assign('sid',$s_id);
            $this->assign('name',$name);
            return $this->fetch('index/list');
        }
    }

    /**
     * 某个商品的评价列表：goodCommList
     * g_id，page
     * @return string
     */
    public function goodCommList(){
        extract(input());
        $this->checkAll();
        //2、此商品的最新的几个评论
        $field = 'comment_id,comment_goodid,comment_user_id,comment_score,comment_content,comment_time';
        $field .= ',comment_username,comment_image,user_username,user_image,user_phone';
        $pl = Db::table('up_good_comment')->alias('gc')
            ->join('up_u_user u','gc.comment_user_id=u.user_id','LEFT')
            ->where('comment_goodid', 'like',"%,$g_id,%")
            ->order('comment_time DESC')
            ->field($field)
            ->page($page,15)
            ->select();
        if(isset($ajax) && $ajax == 1){
            return json(['status'=>200,'msg'=>$pl]);
        }else{
            $this->assign('pl',$pl);
            $this->assign('g_id',$g_id);
            return $this->fetch('index/good_comment');
        }
    }
    /**
     * 某个商铺的所有评价：shopCommList
     * s_id，page
     * @return string
     */
    public function shopCommList(){
        extract(input());
        $this->checkAll();
        if(!isset($page)){
            $page = 1;
        }
        //2、此商品的最新的几个评论
        $field = 'comment_id,comment_goodid,comment_user_id,comment_score,comment_content,comment_time';
        $field .= ',comment_username,comment_image,user_username,user_image,user_phone';
        $pl = Db::table('up_good_comment')->alias('gc')
            ->join('up_u_user u','gc.comment_user_id=u.user_id','LEFT')
            ->where('comment_shopid', $s_id)
            ->order('comment_time DESC')
            ->field($field)
            ->page($page,15)
            ->select();
        if(isset($ajax) && $ajax == 1){
            return json(['status'=>200,'msg'=>$pl]);
        }else{
            $this->assign('pl',$pl);
            $this->assign('s_id',$s_id);
            return $this->fetch('index/shop_comment');
        }
    }
    /**
     * 某个商家的商品详情
     * g_id 商品id
     * @return array 返回数组
     */
    public function shopGoodInfo(){
        extract(input());
        $this->checkAll();
        $user_id = 0;
        //-------------------------------------------------
        $openid = $this->getOpenid();
        $user = Db::table('up_u_user')
            ->where('user_openid', $openid)
            ->find();
        if($user){
            $user_id = $user['user_id'];
        }
        //1、商品信息
        $good = Db::table('up_goods')//存在即代表有抢购商品（秒杀）
        ->where('g_id', $g_id)->find();
        //-------------广告图的商品有可能下架了------------------
        if($good['g_status'] != 1){
            echo "<p style='font-size: 20px;height: 50px;line-height: 50px;width: 100%;text-align: center;'>此商品已下架</p>";
            die;
        }
        //-------------广告图的商品有可能下架了end------------------
        $good['type'] = Db::table('up_shop_type')->where('st_id', $good['g_type'])->value('st_name');
        $good['phone'] = Db::table('up_shops')->where('s_id', $good['g_shop'])->value('s_phone');
        if ($good['g_image']) {
            $good['g_image'] = explode(',', $good['g_image']);
        }
        //2、此商品的最新的几个评论
        $field = 'comment_id,comment_goodid,comment_user_id,comment_score,comment_content,comment_time';
        $field .= ',comment_username,comment_image,user_username,user_image,user_phone';
        $pl = Db::table('up_good_comment')->alias('gc')
            ->join('up_u_user u','gc.comment_user_id=u.user_id','LEFT')
            ->where('comment_goodid', 'like',"%,$g_id,%")
            ->order('comment_time DESC')
            ->field($field)
            ->page(1,3)
            ->select();
        //是不是买了，买了几份（UI上没有，以防万一）
        $have = Db::table('up_shopping_car')
            ->where('car_goodid', $g_id)
            ->where('car_userid', $user_id)
            ->field('car_number')
            ->find();
        $good['haveNum'] = 0;
        if($have){
            $good['haveNum'] = $have['car_number'];
        }
        $this->assign('good',$good);
        $this->assign('pl',$pl);
        return $this->fetch('index/good_info');
    }
    /**
     * 加入购物车
     * g_id 商品id
     * type  1是加 2是减
     * @return array 返回数组
     */
    public function putGoodInCar(){
        extract(input());
        $auth = $this->checkAll();
        //1、商品信息
        $openid = $this->getOpenid();
        $user = Db::table('up_u_user')
            ->where('user_openid', $openid)
            ->find();
        if(!$user){
            return json(['status'=>4,'msg'=>'绑定手机号']);
        }else{
            $user_id = $user['user_id'];
        }
        $g = Db::table('up_goods')
            ->where('g_id', $g_id)
            ->field('g_shop,g_canbuy,g_stock')
            ->find();
        if($g['g_stock']==0){
            return json(['status'=>0,'msg'=>'库存不足']);
        }
        //是不是买了，买了几份（UI上没有，以防万一）
        $buy = Db::table('up_shopping_car')
            ->where('car_goodid', $g_id)
            ->where('car_userid', $user_id)
            ->where('car_status', 1)
            ->find();
        if($buy){
            if($type == 1){
                if($g['g_canbuy']>0 && $buy['car_number'] >= $g['g_canbuy']){
                    return json(['status'=>0,'msg'=>'只能限购'.$g['g_canbuy'].'份']);
                }
                if($g['g_stock']>0 && $buy['car_number'] >= $g['g_stock']){
                    return json(['status'=>0,'msg'=>'库存不足']);
                }
                $res = Db::table('up_shopping_car')
                    ->where('car_goodid', $g_id)
                    ->where('car_userid', $user_id)
                    ->where('car_status', 1)
                    ->setInc('car_number');
            }else{
                if($buy['car_number'] <= 1){
                    $res = Db::table('up_shopping_car')
                        ->where('car_goodid', $g_id)
                        ->where('car_userid', $user_id)
                        ->where('car_status', 1)
                        ->delete();
                }else{
                    $res = Db::table('up_shopping_car')
                        ->where('car_goodid', $g_id)
                        ->where('car_userid', $user_id)
                        ->where('car_status', 1)
                        ->setDec('car_number');
                }
            }
        }else{
            if($type == 1) {
                $data = array();
                $data['car_shopid'] = $g['g_shop'];
                $data['car_goodid'] = $g_id;
                $data['car_userid'] = $user_id;
                $data['car_time'] = date('Y-m-d H:i:s');
                $res = Db::table('up_shopping_car')->insert($data);
            }else{
                return json(['status'=>0,'msg'=>'不能小于0']);
            }
        }
        if($res){
            return json(['status'=>200,'msg'=>'加入购物车成功']);
        }else{
            return json(['status'=>0,'msg'=>'加入购物车失败']);
        }

    }








    public function ask()
    {
        if(!isset($_REQUEST['uid'])){
            echo '参数错误！请从公众号进入';die;
        }
        //-----------------获取微信位置接口-----------------------
        $options = Config::get('wechat');
        $app = new Application($options);
        $js = $app->js;
        $json = $js->config(array('getLocation','chooseImage','uploadImage','downloadImage','getLocalImgData'), false ,false,true);
        $this->assign(['json'=>$json,'uid'=>input('uid')]);
        //-----------------------end-----------------------
        return $this->fetch('my/ruzhu_ask');
    }

    /*
     * 验证手机号唯一性
     */
    public function checkPhone(){
        $phone = input('phone');
        $result = Db::name('counselor')->where('cou_contact_way',$phone)->find();
        if($result){
            return  json(['code'=>200,'msg'=>'该手机号已被申请']);
        }else{
            return  json(['code'=>100,'msg'=>'未被申请']);
        }
    }

    /*
     * 咨询师申请
     */
    public function addCou(){
        extract(input());
        $have = Db::name('counselor')->where('cou_user_id',$uid)->field('cou_id')->find();
        if($have){
            return json(['code'=>100,'msg'=>'请勿重复提交']);
        }else{
            $sola = base64_img($sola)['msg'];
            $arr = [];
            foreach($photo as $key=>$vo){
                $arr[] = base64_img($vo)['msg'];
            }
            $arr = implode (',',$arr);
            $param = [
                'cou_user_id'=>$uid,
                'cou_name'=>$cname,
                'cou_contact_way'=>$way,
                'cou_portrait'=>$sola,
                'cou_papers'=>$arr,
                'cou_apply_explain'=>$explain,
                'cou_audit'=>3,
                'cou_time'=>date('Y-m-d H:i:s'),
                'cou_sheng'=>$sheng,
                'cou_shi'=>$shi
            ];

            $res = Db::name('counselor')->insert($param);
            Db::name('u_user')->where('user_id',$uid)->update(['user_attestation'=>3]);
            if($res){
                return json(['code'=>200,'msg'=>'提交成功']);
            }else{
                return json(['code'=>100,'msg'=>'提交失败']);
            }
        }

    }

    //生成省市区json数据
    public function tuijianCou()
    {
        extract(input());
        //*********************************************************************
        session('target_url',"http://health.xuanwumobile.com/home/Index/tuijianCou");
        $openid = $this->getOpenid();
        //*********************************************************************
        //数据
        $cou_ids = trim($ids,',');//推荐的id的集合
        $where = array();
        $where['cou_audit'] = 2;
        $where['cou_status'] = 1;
        $where['cou_id'] = array('in',$cou_ids);
        $data = Db::table('up_counselor')->alias('c')
            ->join('up_area a','c.cou_shi=a.district_id','LEFT')
            ->where($where)
            ->order('cou_update_time DESC')
//            ->field('c.*,a.district')
            ->select();
        foreach ($data as $k=>$v){
            $data[$k]['cou_good_str'] = $this->getGoodAtStr($v['cou_good']);
        }
        $this->assign('data',$data);
        return $this->fetch('find/tuijian_counselor');
    }
    public function getGoodAtStr($str){
        $str = trim($str,',');
        if($str){
            $arr = explode(',',$str);
            $res = '';
            foreach ($arr as $k){
                if($k == 1){$res .= '亲子教育,';}
                if($k == 2){$res .= '亲密关系,';}
                if($k == 3){$res .= '人际障碍,';}
                if($k == 4){$res .= '情绪障碍,';}
                if($k == 5){$res .= '职业发展,';}
            }
            $res = trim($res,',');
            return $res;
        }else{
            return '';
        }
    }
    //生成省市区json数据
    public function makeJson()
    {
        $pro = Db::table('up_area')
            ->where('pid',1)
            ->field('district_id,district')
            ->select();
        foreach($pro as $k=>$v){

            $city = Db::table('up_area')
                ->where('pid',$v['district_id'])
                ->where('level',2)
                ->field('district_id,district')
                ->select();
            foreach ($city as $kk=>$vv){
                $city[$kk]['subDistrict'] = Db::table('up_area')
                    ->where('pid',$vv['district_id'])
                    ->where('level',3)
                    ->field('district_id,district')
                    ->select();
            }
            $pro[$k]['subDistrict'] = $city;
        }
        return json_encode($pro);
    }
}